<?php

require_once('load.php');
$smarty->assign("s1","sala");
$smarty->assign("s2","");
$smarty->assign("nawig1",array("nazwa" => "Zarządzanie sal","url" => "sala.php"));

if(LOGGED == false)header('Location: ./index.php');


if(isset($_GET['action'])){
	switch ($_GET['action']) {
		
		case 'dodaj_sala':
			// $_get['id'] - identyfikator klienta 
			if(count($_POST)){
				$array= array();	
				$array = $_POST;
				$pojemnosc = $_POST['sala_kolumna']*$_POST['sala_wiersz'];
				$poj2= $pojemnosc;
				for($x=0;$x<$pojemnosc;$x++){
					if($_POST[$x] == 'c'){
						$poj2--;
					}
				}
				unset($array['numer']);
						
				if($sql->query('insert into `sala` (`pojemnosc`, `numer`, `tablica`) values (\''.$poj2.'\',\''.$_POST['numer'].'\',\''.serialize($array).'\') ')){
					header('Location: ./sala.php?action=success&k=dodano_sala');
				}else{
					header('Location: ./sala.php?action=failed');
				}
			}
			$smarty->assign("s2","dodaj");
			break;	
		case 'usun_sala':
			if(isset($_GET['id'])){
				if($sql->query('delete from `sala` WHERE `id_sala`=\''.$_GET['id'].'\'')){
					header('Location: ./sala.php?action=success&k=usunieto_sala');
				}else{
					header('Location: ./sala.php?action=failed');
				}
			}
			break;		
		case 'edytuj_sala':
			if(isset($_GET['id'])){
				
				if(isset($_POST) && count($_POST)){
					$array= array();	
					$array = $_POST;
					$pojemnosc = $_POST['sala_kolumna']*$_POST['sala_wiersz'];
					$poj2= $pojemnosc;
					for($x=0;$x<$pojemnosc;$x++){
						if($_POST[$x] == 'c'){
							$poj2--;
						}
					}
					unset($array['numer']);

					if($sql->query('UPDATE `sala` SET `pojemnosc`=\''.$poj2.'\', `numer`=\''.$_POST['numer'].'\', `tablica`=\''.serialize($array).'\' WHERE `id_sala`=\''.$_GET['id'].'\' ')){
						header('Location: ./sala.php?action=success&k=zaktualizowano_sala');
					}else{
						header('Location: ./sala.php?action=failed');
					}
				}
				$ar = $sql->QueryGetFetch("SELECT * FROM `sala` WHERE `id_sala`='".$_GET['id']."'");
				$ar = $ar[0];
				$ar['tablica'] = unserialize($ar['tablica']);
				$smarty->assign("sala",$ar);
				$smarty->assign("s2","edytuj_sala");
			}		
			break;
		case 'failed':
		
			if(isset($_GET['k'])){
				if($_GET['k'] == 'usunieto_sala'){
					$smarty->assign("komunikat","Usunięto");
				}else{
					$smarty->assign("komunikat","Wstąpił błąd");
				}
			}else{
				$smarty->assign("komunikat","Wstąpil błąd");
			}
			
			break;
		case 'success':
		
			if(isset($_GET['k'])){
				if($_GET['k'] == 'dodano_sala'){
					$smarty->assign("komunikat","Pomyslnie dodano sala");
				}else if($_GET['k'] == 'usunieto_sala'){
					$smarty->assign("komunikat","Usunięto");
				}else{
					$smarty->assign("komunikat","Pomyslnie zakonczono");
				}
			}else{
				$smarty->assign("komunikat","Pomyslnie zakonczono");
			}
			break;
		default:

			break;
	}
}

$smarty->assign("sale",$sql->QueryGetFetch("select * from `sala`"));

$system->dodaj_js('skrypty.js');
$smarty->display('templates/index.tpl');
?>